using System.Drawing;

// kesovat se bude do obrazku.
// bud si to tam vytvorim, nebo si to nastavim
// nastaveni si bude trida provadet sama, bude pouzivat metody Settingu
// moznosti budou: image, gradient a color
// pak mozna pribude strected image

namespace MDAControlsFramework.Helpers {
	/// <summary>
	/// trida spravujici pozadi
	/// da se vytvorit v ruznych variantach
	/// </summary>
	public class MDABackground: IMDAObject {
		private Color _backgroundColor = Color.Empty;
		private Image _backgroundImage = null;
		private MDAGradientStruct _backgroundGradient = new MDAGradientStruct();
		private bool _disableOnce = false;
		private string _colorName = "";

		public static MDABackground None;

		public MDABackground() {
		}

		public MDABackground(Color color, Image image, MDAGradientStruct gradientStruct) {
			_backgroundColor = color;
			_colorName = color.ToString();
			_backgroundImage = image;
			_backgroundGradient = gradientStruct;
		}

		public object Clone() {
			MDABackground clone = this.MemberwiseClone() as MDABackground;
			if (_backgroundImage != null) {
				clone._backgroundImage = _backgroundImage.Clone() as Image;
			}
			clone._backgroundGradient = _backgroundGradient;
			return clone;
		}

		public Color BackgroundColor {
			get {
				return _backgroundColor;
			}
			set {
				_backgroundColor = value;
			}
		}

		public Image BackgroundImage {
			get {
				return _backgroundImage;
			}
			set {
				_backgroundImage = value;
			}
		}

		/// <summary>
		/// Disable painting only for next paint
		/// </summary>
		public bool DisableOnce {
			get {
				return _disableOnce;
			}
			set {
				_disableOnce = value;
			}
		}

		public string GetName() {
			return "null";
		}

		// TODO mozna pridat kesovani do image
		public virtual void Draw(Graphics g, Rectangle rect) {
			if (_disableOnce) {
				_disableOnce = false;
				return;
			}

			if (!BackgroundColor.IsEmpty) {
				g.FillRectangle(new SolidBrush(BackgroundColor), rect);
				return;
			}
			if (BackgroundImage != null) {
				Rectangle imgRecr = new Rectangle(0, 0, BackgroundImage.Width, BackgroundImage.Height);
				g.DrawImage(BackgroundImage, rect, imgRecr, GraphicsUnit.Pixel);
				return;
			}
			if (!Equals(_backgroundGradient, MDAGradientStruct.Empty)) {
				GradientFill.Fill(g, rect, _backgroundGradient);
				return;
			}
		}

		#region IMDAObject Members

		public bool AddObject(IMDAObject objectInstance) {
			return false;
		}

		public bool SetProperty(string name, string value) {
			if (name == "backgroundcolor") {
				_backgroundColor = Parser.parseColor(value);
				_colorName = value;
				return true;
			}
			return false;
		}

		public void AddEvent(string eventName, string eventHandler) {
			return;
		}

		#endregion

		public override string ToString() {
			return "{Background " + _colorName + "}";
		}
	}
}